<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectCustomerType from '@/components/select/SelectCustomerType'
import SelectIndustrialClass from '@/credit/components/select/SelectIndustrialClass.vue'
import { phoneValidator, registerNoValidator, idCardValidator } from '@/common/plugins/validator'
import SelectAddress from '@/components/select/SelectAddress'
import SelectMainBusiness from '@/credit/components/select/SelectMainBusiness.vue'
// import SelectOrgCredit from '@/credit/views/county/unionProvince/components/item/SelectCredit'
import { enumGuatype } from '@/credit/views/city/reportCity/reportMixins'
import { enumProjectType } from '@/bigcredit/views/program/enum'
import { enumFourModernType, enumEntProperty } from '@/bigcredit/views/program/enum'
// import { enumUnGuaType } from '@/credit/views/county/loan/component/un/index'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value)) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    const empValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (+value < 0 || value % 1 !== 0) {
        callback(new Error('人数需填写正整数'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      tableName: 'market_project',
      isCommon: false,
      formItems: [
        {
          tag: 'select',
          itemAttrs: {
            label: '项目类型',
          },
          attrs: {
            clearable: false,
            key: 'project_type',
            value: '',
            options: enumProjectType,
          },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '客户名称',
            // rules: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
          },
          attrs: { key: 'customer_name', value: '', placeholder: '请输入客户名称' },
        },
        {
          tag: SelectCustomerType,
          itemAttrs: {
            label: '客户类型',
            // rules: { required: true, message: '请选择客户类型', trigger: 'change' },
          },
          attrs: { clearable: false, key: 'org_class', class: 'form-control-ml' },

          events: {
            select: (model, v, item) => {
              this.isCommon = +item.customerType === 1
              model.org_class_name = item?.orgClassName || ''
            },
          },
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '企业属性',
          },
          attrs: {
            key: 'ent_property',
            value: '',
            options: enumEntProperty,
          },
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '四化类型',
          },
          attrs: {
            key: 'four_modern_type',
            value: '',
            options: enumFourModernType,
          },
        },
        {
          tag: SelectAddress,
          itemAttrs: { label: '主体地址' },
          attrs: {
            key: 'address_code',
            value: '',
            placeholder: '请主体地址',
            class: 'form-control-ml',
            autoTop: true,
          },
        },
        {
          tag: 'input',
          itemAttrs: { label: '详细地址' },
          attrs: { key: 'address_detail', value: '', placeholder: '请输入详细地址' },
        },
        {
          tag: 'input',
          attrs: { key: 'org_class_name' },
          ifShow: false,
        },
        {
          tag: 'radio-group',
          itemAttrs: {
            label: '是否为十二大产业',
          },
          attrs: {
            clearable: false,
            key: 'is_ind12',
            value: '',
            options: {
              0: '否',
              1: '是',
            },
          },
        },
        {
          tag: SelectMainBusiness,
          itemAttrs: {
            label: '十二大产业',
            rules: [{ required: true, message: '请选择十二大产业', trigger: 'change' }],
          },
          attrs: {
            key: 'main_business',
            class: 'form-control-ml',
            placeholder: '请选择十二大产业',
          },
          ifRender: model => +model.is_ind12 === 1,
        },
        {
          tag: SelectIndustrialClass,
          itemAttrs: {
            label: '行业分类',
          },
          attrs: {
            filterable: true,
            clearable: false,
            key: 'parent_main_business_code',
            class: 'form-control-ml',
          },

          events: {
            select: (model, value, select) => {
              model.parent_main_business = select.ind_class_name || ''
            },
          },
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '营业执照',
            rules: [
              {
                validator: registerNoValidator,
                message: '请输入正确的营业执照',
                trigger: 'blur',
              },
            ],
          },
          attrs: { key: 'register_no', value: '', placeholder: '请输入信息' },
          ifRender: () => this.isCommon,
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '负责人名称',
            // rules: { required: true, message: '请输入负责人名称', trigger: 'blur' },
          },
          attrs: { key: 'corp_name', value: '', placeholder: '请输入负责人名称' },

          ifRender: () => this.isCommon,
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '身份证号码',
            rules: [
              // { required: true, message: '请输入身份证号码', trigger: 'blur' },
              {
                validator: idCardValidator,
                message: '请输入正确的身份证号码',
                trigger: 'blur',
              },
            ],
          },
          attrs: { key: 'lender_idcard', value: '', placeholder: '请输入信息' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系电话',
            rules: [
              // { required: true, message: '请输入手机号码', trigger: 'blur' },
              {
                validator: phoneValidator,
                message: '请输入正确的手机号码',
                trigger: 'blur',
              },
            ],
          },
          attrs: { key: 'corp_phone', value: '', placeholder: '联系电话' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '销售收入',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'sale_income', value: '', placeholder: '请输入销售收入' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '资产总额',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'assets_funds', value: '', placeholder: '请输入资产总额' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '从业人数',
            rules: [
              // { required: true, message: '请输入从业人数', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
              { validator: empValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'emp_total', value: '', placeholder: '请输入从业人数' },
          tagSlots: [{ name: 'append', slot: '人' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '上年度纳税额',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'tax_funds', value: '', placeholder: '请输入上年度纳税额' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '担保金额',
            rules: [
              { required: true, message: '请输入担保金额', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'warranty_amount', value: '' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '贷款利率',
            rules: [
              // { required: true, message: '请输入贷款利率', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'load_rate', value: '' },
          tagSlots: [{ name: 'append', slot: '%' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '借款合同号',
          },
          attrs: { key: 'gua_contract', value: '' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '保证合同编号',
          },
          attrs: { key: 'ensure_contract', value: '' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '委托担保合同号',
          },
          attrs: { key: 'entrust_contract', value: '' },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '担保责任发生日期',
            rules: [{ required: true, message: '请选择担保责任发生日期', trigger: 'change' }],
          },
          attrs: { key: 'load_date', value: '', placeholder: '请选择担保责任发生日期' },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '担保责任解除日期',
            rules: [{ required: true, message: '请选择担保责任解除日期', trigger: 'change' }],
          },
          attrs: { key: 'load_end_date', value: '', placeholder: '请选择担保责任解除日期' },
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '贷款期限',
            rules: [
              { required: true, message: '请输入贷款期限', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: {
            key: 'warranty_period',
            value: '',
            placeholder: '请输入贷款期限',
          },
          tagSlots: [{ name: 'append', slot: '月' }],
        },

        {
          tag: 'textarea',
          itemAttrs: { label: '反担保措施' },
          attrs: {
            key: 'warranty_method',
            value: '',
            placeholder: '请输入反担保措施',
            autosize: { minRows: 3 },
          },
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '业务模式',
          },
          attrs: {
            key: 'gua_type',
            value: '',
            options: enumGuatype,
          },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '贷款银行',
            rules: [
              // { required: true, message: '请输入风险分担比例', trigger: 'blur' },
            ],
          },
          attrs: {
            key: 'bank_name',
            value: '',
            placeholder: '请输入贷款银行名称',
          },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '银行分险比列',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: {
            key: 'bank_risk_rate',
            value: '',
            placeholder: '请输入银行分险比列',
          },
          tagSlots: [{ name: 'append', slot: '%' }],
        },
        {
          tag: 'input',
          itemAttrs: { label: '联保机构' },
          attrs: {
            key: 'recommend_service_name',
            value: '',
            placeholder: '请输入联保机构名称',
          },
          ifRender: model => +model.gua_type === 4,
        },
        // {
        //   tag: SelectOrgCredit,
        //   itemAttrs: {
        //     label: '联保机构',
        //     rules: [{ required: true, message: '请选择联保机构', trigger: 'change' }],
        //   },
        //   attrs: {
        //     class: 'form-control-ml',
        //     key: 'recommend_service',
        //     value: '',
        //   },
        //   events: {
        //     select: (model, select) => {
        //       model.recommend_service_name = select?.agency_name ?? ''
        //     },
        //   },
        //   ifRender: model => +model.gua_type === 1,
        // },
        {
          tag: 'input',
          itemAttrs: {
            label: '风险分担比例',
            rules: [
              // { required: true, message: '请输入风险分担比例', trigger: 'blur' },
            ],
          },
          attrs: {
            key: 'risk_rate',
            value: '',
            placeholder: '请输入风险分担比例',
          },
        },
        {
          tag: 'radio-group',
          itemAttrs: {
            label: '是否为扶贫项目',
            // rules: [{ required: true, message: '是否为扶贫项目', trigger: 'change' }],
          },
          attrs: {
            clearable: false,
            key: 'is_poverty_project',
            value: '',
            options: {
              0: '否',
              1: '是',
            },
          },
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '综合担保费率',
            rules: [
              // { required: true, message: '请输入综合担保费率', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: {
            key: 'warranty_rate',
            value: '',
            placeholder: '请输入综合担保费率',
          },
          tagSlots: [{ name: 'append', slot: '%' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '综合担保费金额',
            rules: [
              // { required: true, message: '请输入综合担保费率', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: {
            key: 'warranty_fee',
            value: '',
            placeholder: '请输入综合担保费金额',
          },
          tagSlots: [{ name: 'append', slot: '元' }],
        },

        {
          tag: 'textarea',
          itemAttrs: { label: '贷款用途' },
          attrs: {
            key: 'funds_use_dir',
            value: '',
            placeholder: '请输入贷款用途',
            autosize: { minRows: 3 },
          },
        },

        {
          tag: 'textarea',
          itemAttrs: { label: '备注' },
          attrs: {
            key: 'remark',
            value: '',
            placeholder: '备注',
            autosize: { minRows: 3 },
          },
        },
        // {
        //   tag: 'select',
        //   itemAttrs: {
        //     label: '解保类型',
        //   },
        //   attrs: {
        //     key: 'un_gua_type',
        //     value: '',
        //     options: { 0: '在保', ...enumUnGuaType },
        //     placeholder: '请选择解保类型',
        //   },
        // },
        // {
        //   tag: 'date',
        //   itemAttrs: {
        //     label: '解保时间',
        //     rules: [{ required: true, message: '请选择解保日期', trigger: 'change' }],
        //   },
        //   attrs: { key: 'un_gua_time', value: '', placeholder: '请选择解保时间' },
        //   ifRender: model => +model.un_gua_type !== 0,
        // },
      ],
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId', 'org']),
  },
  watch: {
    visible(val) {
      this.innerVisible = val
      this.isCommon = +this.formData.customer_type === 1
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      Object.keys(model).forEach(item => {
        model[item] = model[item] || null
      })
      model.main_business = +model.is_ind12 === 1 ? model.main_business : ''
      // model.register_no = this.isCommon ? model.register_no : ''
      model.corp_name = this.isCommon ? model.corp_name : model.customer_name
      model.customer_type = this.isCommon ? 1 : 2
      model.service_name = this.org.orgName
      model.service_id = this.clientId
      if (!this.tableId) {
        model.is_history = 1
      }
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
